<?php

namespace app\api\controller;
use think\Db;
class Bendi extends \app\api\controller\Base {
    /**
     * getLocalCircle 获取本地圈内容
     * @param $user_id 用户id
     * @param $time 时间
     * @param $value 加密值
     * @param page 页码
     * @param $shi 市id
     * @return string|\think\response\Json
     */
    public function getLocalCircle(){
        extract(input());
        $auth = $this->checkAll($user_id,$time,$value,$reqKey);
        if($auth == false){
            $this->apiError(301);
        }
        $data = Db::table('up_article')
            ->alias('ua')
            ->join('up_u_user uu','ua.a_user_id=uu.user_id','left')
            ->where('a_shi',$shi)
            ->whereTime('a_createtime','-24 hours')
            ->field('uu.user_id,uu.user_username,uu.user_image,ua.a_id,ua.a_title,ua.a_image,ua.a_num,ua.a_createtime,ua.a_type')
            ->page($page,10)
            ->order('ua.a_createtime desc')
            ->select();
        $this->apiSuccess($data);
    }



    public function alipay(){
        extract(input());
        $auth = $this->checkAll($user_id,$time,$value,$reqKey);
        if($auth == false){
            $this->apiError(301);
        }
        $long = Db::table('up_user_auth_long')
            ->where('long_id',$long_id)->find();
        $data = array();
        $data['order_number'] = time().rand(1000000,9999999);
        $data['order_user_id'] = $user_id;
        $data['order_long_id'] = $long_id;
        $data['order_datetime'] = date('Y-m-d H:i:s');
        $data['order_type'] = 2;
        $data['order_money'] = $long['long_money'];
        $data['order_long_type'] = $long['long_type'];
        $res = Db::table('up_order')->insert($data);
        if($res){
            $respont = $this->getParam($data['order_number'],$data['order_money']);
            return json_encode($respont);
        }else{
            return json_encode(['status'=>0,'msgsss'=>'生成订单失败，请联系管理员']);
        }
    }
    public function huidiao()
    {
        $getInfo = $this->checkReturn();
        if($getInfo){
            if($getInfo['trade_status'] == 'TRADE_SUCCESS' || $getInfo['trade_status'] == 'TRADE_FINISHED'){
                $num = $getInfo['out_trade_no'];
                $dataOrder = array();
                $dataOrder['order_trade_no'] = $getInfo['trade_no'];
                $dataOrder['order_boos_get'] = $getInfo['receipt_amount'];
                $dataOrder['order_user_pay'] = $getInfo['buyer_pay_amount'];
                $dataOrder['order_ali_fund_bill_list'] = $getInfo['fund_bill_list'];
                $dataOrder['order_ali_paytime'] = $getInfo['gmt_payment'];
                $dataOrder['order_status'] = 1;
                Db::startTrans();
                try{
//                Db::table('up_order')->where('order_number',$num)->update($dataOrder);
//                if($res){
                    $change = Db::table('up_order')->where('order_number',$num)->field('order_user_id,order_long_type,order_long_id')->find();
                    $have = Db::table('up_user_auth')
                        ->where('uauth_user_id',$change['order_user_id'])
                        ->where('uauth_type',$change['order_long_type'])
                        ->find();
                    $today = date('Y-m-d');
                    if($have){
                        $data = array();
                        $data['uauth_status'] = 1;
                        $plus = Db::table('up_user_auth_long')->where('long_id',$change['order_long_id'])->value('long_how');
                        if($have['uauth_end'] >= $today){
                            //========使用期限========
                            $dataOrder['order_start'] = $have['uauth_end'];
                            //=========end=======
                            $data['uauth_end'] = date("Y-m-d",strtotime("+$plus month",strtotime("{$have['uauth_end']}")));
                        }else{
                            //========使用期限========
                            $dataOrder['order_start'] = $today;
                            //=========end=======
                            $data['uauth_start'] = $today;
                            $data['uauth_time'] = date('Y-m-d H:i:s');
                            $data['uauth_end'] = date("Y-m-d",strtotime("+$plus month",strtotime("{$today}")));
                        }
                        //========使用期限========
                        $dataOrder['order_end'] = $data['uauth_end'];
                        //=========end=======
                        Db::table('up_user_auth')
                            ->where('uauth_user_id',$change['order_user_id'])
                            ->where('uauth_type',$change['order_long_type'])->update($data);
                    }else{
                        $plus = Db::table('up_user_auth_long')->where('long_id',$change['order_long_id'])->value('long_how');
                        $data = array();
                        $data['uauth_user_id'] = $change['order_user_id'];
                        $data['uauth_long_id'] = $change['order_long_id'];
                        $data['uauth_type'] = $change['order_long_type'];
                        $data['uauth_start'] = $today;
                        $data['uauth_end'] = date("Y-m-d",strtotime("+$plus month",strtotime("{$today}")));
                        $data['uauth_time'] = date('Y-m-d H:i:s');
                        //========使用期限========
                        $dataOrder['order_start'] = $today;
                        $dataOrder['order_end'] = $data['uauth_end'];
                        //=========end=======
                        Db::table('up_user_auth')->insert($data);
                    }
                    Db::table('up_order')->where('order_number',$num)->update($dataOrder);
                    //=======================入消息库===============================
                    $pai[2] = '铜';
                    $pai[3] = '银';
                    $pai[4] = '金';
                    $notice = array();
                    $notice['n_title'] = '购买权限成功';
                    $notice['n_content'] = "您已经成功购买{$pai[$change['order_long_type']]}牌召集令，快来使用吧";
                    $notice['n_createtime'] = date('Y-m-d H:i:s');
                    $notice['n_type'] = 3;
                    $notice['n_send_type'] = 2;
                    $notice['n_user_id'] = $change['order_user_id'];
                    $notice['n_read_users'] = ',0,';
                    $notice['n_clear_users'] = ',0,';
                    Db::table('up_notice')->insert($notice);
                    //========================入消息库end==============================
                    Db::commit();
                    $this->Jpush("{$change['order_user_id']}", '购买权限成功', 1, '');
                    die('success');
//                }
                }catch (\Exception $e) {
                    Db::rollback();
                    return 'error';
                }
            }
        }
    }
}